package com.swd.order.controller;

import com.github.pagehelper.Page;
import com.swd.order.model.Category;
import com.swd.order.model.Foods;
import com.swd.order.service.FoodsService;
import com.swd.order.util.PageVo;
import com.swd.order.vo.CategoryVo;
import com.swd.order.service.CategoryService;
import com.swd.order.vo.Ret;
import com.swd.order.vo.Rets;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.select.Limit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("category")
@Slf4j
public class CategoryContoller {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private FoodsService foodsService;

    @GetMapping(value = "queryAll",produces = "application/json")
    private Ret queryAll(){
        List<Category> categoryVos = categoryService.all();
        return Rets.success(categoryVos);
    }

    @GetMapping(value = "allFoods",produces = "application/json")
    private Ret allFoods(){
        List<CategoryVo> categoryVos = categoryService.queryAll();

        categoryVos = categoryVos.stream().filter(item-> item.getFoods().size()>0).collect(Collectors.toList());

        return Rets.success(categoryVos);
    }

    @PostMapping(value = "allFoodsPageAble",produces = "application/json")
    private Ret allFoodsPageAble(int pageNum,int limit){
        Page<CategoryVo> page = categoryService.queryAllPageAble(pageNum, limit);
        return Rets.success(PageVo.build(page));
    }

    @PostMapping("save")
    public Ret save(@RequestBody Category category){

        if(StringUtils.isEmpty(category.getCategoryName())){
            return Rets.failure("名称不能为空");
        }

        if(category.getIndex() == null){
            return Rets.failure("序号不能为空");
        }

        if (categoryService.queryByName(category.getCategoryName()).size()>0){
            return Rets.failure("该分类名称已存在。");
        }

        boolean success = categoryService.save(category);

        if (success){
            return Rets.success(category);
        }else {
            return Rets.failure("保存失败");
        }

    }



    @PostMapping("update")
    public Ret update(@RequestBody Category category){

        if (category.getId() == null){
            return Rets.failure("该分类不存在，无法修改。");
        }

        if (categoryService.queryById(category.getId())==null){
            return Rets.failure("该分类不存在，无法修改。");
        }

        log.info("更新category:{}",category);
        boolean sucess = categoryService.update(category);

        if (sucess){
            return Rets.success(category);
        }else {
            return Rets.failure("更新失败");
        }
    }

    @PostMapping("delete")
    public Ret delete(int id){



        Page<Foods> query = foodsService.query(id, null, 1, 10);

        if (query.getTotal() > 0 ){
            return Rets.failure("该分类下仍有菜品，不能删除");
        }

        boolean success = categoryService.delete(id);

        if (success){
            return Rets.success();
        }else {
            return Rets.failure("删除失败");
        }
    }

}
